function getSetting (resolve, reject, options = {}) {
    wx.openSetting({
        success: (res) => {
            if (res.authSetting[options.auth]) {
                resolve(options);
            }
            else {
                wx.showModal({
                    title: '温馨提示',
                    content: options.content,
                    success: (result) => {
                        if (result.confirm) {
                            getSetting(resolve, reject, options);
                        }
                        else if (result.cancel) {
                            reject(options)
                        }
                    }
                })
            }
        }
    })
}

function authorize (options = {}) {
    return new Promise((resolve, reject) => {
        wx.getSetting({
            success: (res) => {
                if (res.authSetting[options.auth]) {
                    resolve(options);
                }
                else {
                    wx.authorize({
                        scope: options.auth,
                        success(res) {
                            resolve(options);
                        },
                        fail() {
                            getSetting(resolve, reject, options);
                        }
                    })
                }
            }
        })
    })
}

export default authorize;